<?php

namespace app\yperp\logic\ypapp;

use app\yperp\common\Tools;
use GuzzleHttp\Client as GuzzleHttpClient;

class CheckGoods
{

    private static $instance;
    public static function getInstance()
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
 
    function Request($url, $param, $method = 'POST', $token = '')
    {
        if ($token == '') {
            $token = module_config('yperp.jst_token');
        }
        $data = Tools::ypapp($param, $token);
        // dump($data);
        $client = new GuzzleHttpClient([
            'verify' => env('root_path') . 'application\yperp\common\cacert.pem',
            // 忽略证书验证
        ]);
        $response = $client->request($method, $url, [
            'json' => $data['body'],
            'headers' => [
                'Content-Type' => 'application/json',
                'sign' => $data['headers']['sign'],
                'timestamp' => $data['headers']['timestamp'],
                'token' => $token,
                'xweb_xhr' => 1
            ]
        ]);
        $data = $response->getBody()->getContents();
        return json_decode($data, true);
    }

    public function queryGoodsInfoById($goods_id)
    {
        $param = [
            "shopId" => "7956058544221398",
            "goodsId" => $goods_id,
            "activityCode" => "",
            "activityId" => ""
        ];
        $url  = 'https://ypapp.changhong.com/platform-frame-wechat-app/wechatapp/goods/v1/queryGoodsInfoById';
        $data =    $this->Request($url, $param);
        if (count($data['body'])) {
            // dump($data['body']['marketPrice']);
            // dump($data['body']['costPrice']);
            // dump($data['body']['sellPrice']);
            // dump($data['body']['goodsCode']);
            // dump($data['body']['name']);
            // dump($data['body']['note']);
            return [
                'marketPrice' => $data['body']['marketPrice'],
                'costPrice' => $data['body']['costPrice'],
                'sellPrice' => $data['body']['sellPrice'],
                'goodsCode' => $data['body']['goodsCode'],
                'name' => $data['body']['name'],
                'note' => $data['body']['note'],
            ];
        } else {
            return false;
        }
    }
}
